//
//  GeDiaoTableview.m
//  Style
//
//  Created by dllo on 15/9/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#define ScreenWidth          [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight         [[UIScreen mainScreen] bounds].size.height
#define ViewWidth(v)                        v.frame.size.width
#define ViewHeight(v)                       v.frame.size.height

#import "GeDiaoTableview.h"
#import "GeDiaoCell.h"
#import <AFNetworking.h>
#import "AFNetWorkingW.h"

@implementation GeDiaoTableview

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        
        /* 设置UITableView */
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        self.bounces = NO;
        self.delegate = self;
        self.dataSource = self;
        

        [self registerClass:[GeDiaoCell class] forCellReuseIdentifier:@"reuse"];
        
        /* 设置小菊花 */
        self.activity = [[UIActivityIndicatorView alloc] init];
        self.activity.color = [UIColor blackColor];
        self.activity.frame = CGRectMake(0, 0, 80, 80);
        self.activity.center = self.center;
        [self addSubview:self.activity];
        [self.activity startAnimating];
        
        self.mArr = [NSMutableArray array];
        
        
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.GEDIAOModel = [self.mArr objectAtIndex:indexPath.row];
    
    self.GEDIAOCell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    self.GEDIAOCell.GEDIAOModel = self.GEDIAOModel;
    
    return self.GEDIAOCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.GEDIAOCell.strContentLabel.text.length >= 90)
    {
        return ScreenHeight + 80;
    } else
    {
        return ScreenHeight + 50;
    }
}

- (void)dataHandle
{
    NSLog(@"qwertyu     ");
    
    [AFNetWorkingW netWorkingHanderGetGataWithUrl:self.UrlStr resultBlock:^(id result) {
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
#pragma mark ** 本地缓存
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        if (result != nil) {
            
            /* 有网归档 */
            if (self.mArr.count == 0) {
                
                self.mDic = result;
                NSDictionary *dic = [self.mDic objectForKey:@"hpEntity"];
                
                self.GEDIAOModel = [[GeDiaoModel alloc] init];
                [self.GEDIAOModel setValuesForKeysWithDictionary:dic];
                
                [self.mArr addObject:self.GEDIAOModel];
                
                NSNotificationCenter *urlCenter = [NSNotificationCenter defaultCenter];
                [urlCenter postNotificationName:@"url" object:self.GEDIAOModel.sWebLk];
                
                [self reloadData];
                
                NSString *modelPath = [path stringByAppendingFormat:@"/main%d",self.modelCount];
                [NSKeyedArchiver archiveRootObject:self.GEDIAOModel toFile:modelPath];
                
                [self.activity stopAnimating];
            }
        }
        else
        {
            
            NSString *modelPath = [path stringByAppendingFormat:@"/main%d",self.modelCount];
            GeDiaoModel *Model = [NSKeyedUnarchiver unarchiveObjectWithFile:modelPath];
            if (Model != nil) {
                [self.mArr addObject:Model];
            }
            [self reloadData];
            
        }

    }];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
